<?php
namespace App\Controller;

use App\Model\DynamicModel;
use lib\View;
use App\Model\UserModel;
use App\Model\CommentModel;
use App\Model\FriendsModel;
use App\Model\MailModel;


class MailController
{	

	public function mail(){
		   $user= isset($_SESSION['user']) ? $_SESSION['user']  : url('login');
   		$user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id']  : '';
   		$choose = isset($_GET['choose']) ? $_GET['choose']  : '';
      $pn=isset($_GET['pn']) ? $_GET['pn']  : 1;

      $edit = new UserModel();

   		$data = $edit->get(['username' => $user]);
  		$_SESSION['pic'] = $data[0]['pic']; 

      //获取好友信息
      $get_Data =new FriendsModel();
      $Data = $get_Data->friends();
      $userData = $Data[0];
      $fData = $Data[1];


        //获取邮箱信息
        $mails = new MailModel();
        $emails = $mails->get(['user_id'=>$user_id],'status');
        //获取网页页顶信息框的数据
        $count=$mails->count_email($emails);

        $ls = Self::selecte_mail($choose,$emails);
         
           if (!empty($ls)==true) {
            $list = $mails->pangination($user_id,$ls);
            $emails = $list['messages'];
          }
          else{
            $list['pageCount']=1;
            $pn=1;
            $emails=$ls;
          }

        //设置表的显示方式
   		return new View('mail',['pn'=>$pn,'user'=>$user,'user_id'=>$user_id,'data'=>$data[0],'userData'=>$userData,'fData'=>$fData,'emails'=>$emails,'count'=>$count,'pageCount'=>$list['pageCount'],'choose'=>$choose]);
		}

  public function mail_e(){
   			return new View('mail_e');
   }

  public function send_mail(){
       		//获取基本信息
       		$user_id = $_SESSION['user_id'];
       		$content= $_POST['content'];
       		$title = $_POST['title'];
          $email_id =isset($_POST['email_id'])? $_POST['email_id'] : '';
       		//接收用户的邮箱
       		$Addressee=$_POST['Addressee'];
       		
       		//获取接收用户的信息
       		$users = new UserModel();
       		$get_user =$users->get(['email'=>$Addressee])[0];
       		$get_user_id=$get_user['id'];
       		//获取发送用户的信息
       		$send_user = $users->get(['id'=>$user_id])[0];
       		$send_user_email = $send_user['email'];



       		$mails = new MailModel();
          
          if (!empty($email_id)) {
          $target_email_status = $mails->get(['id'=>$email_id]);
          
          //判断是不是已经回复过的邮件
          if ($target_email_status[0]['status'] !=2) {
          $recover_mail['status'] = 2;
          $recover_mail['id'] = $email_id;
          $mails->save($recover_mail);
          }
          }

       		$mail['title'] = $title;
       		$mail['content'] = $content;
       		$mail['user_id'] = $get_user_id;
       		$mail['send_email'] = $send_user_email;
       		$mail['status'] = 0 ;
       		$mail['create_at'] = date('Y-m-d H:i:s',time());
       		$mails->save($mail);

   		success('邮件发送成功','mail');
   }

   public function mail_recover(){
         $user= isset($_SESSION['user']) ? $_SESSION['user']  : url('login');
        $user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id']  : ''; 
        $email_id = isset($_GET['email_id']) ? $_GET['email_id']  : '';
      
        $edit = new UserModel();
        $data = $edit->get(['username' => $user]);
        $_SESSION['pic'] = $data[0]['pic']; 
          //获取好友信息
          $get_Data =new FriendsModel();
          $Data = $get_Data->friends();
          $userData = $Data[0];
          $fData = $Data[1];
       
        //获取邮箱信息
        $get_email_Data = new MailModel();
        //获取页面页顶的未读邮件信息
        $emails = $get_email_Data->get(['user_id'=>$user_id]);
        $count=$get_email_Data->count_email($emails);  
        $_SESSION['count'] = $count;

        $email = $get_email_Data->get(['id'=>$email_id])[0];

        return new View('mail_recover',['user'=>$user,'user_id'=>$user_id,'data'=>$data[0],'userData'=>$userData,'fData'=>$fData,'email'=>$email,'count'=>$count]);
         }


   public static function deal_mail(){

        $email_id=$_GET['id'];
        $deal_choose=$_GET['deal_choose'];
       $get_email_Data = new MailModel();
         //进入邮件详情界面
         if ($deal_choose == 1) {
        $user = isset($_SESSION['user']) ? $_SESSION['user']  : '';  
        $user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id']  : ''; 
        $choose = isset($_GET['choose']) ? $_GET['choose']  : '';
        $edit = new UserModel();
        $data = $edit->get(['username' => $user]);
        $_SESSION['pic'] = $data[0]['pic']; 
          //获取好友信息
          $get_Data =new FriendsModel();
          $Data = $get_Data->friends();
          $userData = $Data[0];
          $fData = $Data[1];
        //获取邮箱信息
      
    
        $email = $get_email_Data->get(['id'=>$email_id]);
        if ($email[0]['status'] == 0 ) {
            $email[0]['status'] = 1;  
            $get_email_Data->save($email[0]);
        } 
        $emails = $get_email_Data->get(['user_id'=>$user_id]);
        $count=$get_email_Data->count_email($emails);  
        $_SESSION['count'] = $count;

        return new View('mail_detail',['user'=>$user,'user_id'=>$user_id,'data'=>$data[0],'userData'=>$userData,'fData'=>$fData,'email'=>$email[0],'count'=>$count]);
         }

         //删除邮件
         if ($deal_choose == 2) {
          //将邮件放入回收站，修改邮件状态
       
              $email=$get_email_Data->get(['id'=>$email_id]);
              $email['status'] = 3;
          // 存入数据库
              $data['status']= $email['status'];
              $data['id'] = $email_id;
              $get_email_Data->save($data);
              success('成功将此邮件放入回收站','mail');

         }

         if ($deal_choose == 3) {

          //彻底删除邮件
         $get_email_Data->get(['id'=>$email_id]);
         $data['id'] = $email_id;
         $data['status'] = 0;
         $get_email_Data->save($data);
         success('邮件恢复成功','mail');
        }

         //彻底删除邮件
        if ($deal_choose == 4) {
          //彻底删除邮件
      
         $data['id']=$email_id;
         $get_email_Data->deletes($data);
         success('删除邮件成功','mail');
        }
   }

   private static function selecte_mail($choose,Array $emails){
    foreach ($emails as $key => $value) {
        if ($choose == '') {
          continue;
        }
        elseif ($choose != $value['status']) {
            unset($emails[$key]);
            }
     }
        return $emails;
  }

}
